package com.blog.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.blog.auth.dao.AuthMenuDao;
import com.blog.auth.dao.AuthUserDao;
import com.blog.auth.pojo.AuthMenu;
import com.blog.auth.pojo.AuthUser;
import com.blog.auth.service.AuthMenuService;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 这里分页使用的是pagehelper
 */
@Service
@Transactional(readOnly = false)
public class AuthMenuServiceImpl implements AuthMenuService {

    @Autowired
    private AuthMenuDao authMenuDao;

    @Autowired
    private AuthUserDao authUserDao;

    @Override
    public List<AuthMenu> selectPermissionUserId(String userId) {
        List<AuthMenu> selectPermissionList = null;
        if (this.isSysAdmin(userId)) {
            // 如果是系统管理员 admin，获取所有权限
            QueryWrapper wrapper = new QueryWrapper();
            selectPermissionList = authMenuDao.selectList(null);
        } else {
            selectPermissionList = authMenuDao.getPermission(userId);
        }
        return selectPermissionList;
    }

    private boolean isSysAdmin(String userId) {
        AuthUser user = authUserDao.selectById(userId);
        if (null != user && "admin".equals(user.getUsername())) {
            return true;
        }
        return false;
    }
}